package com.cheung.leetcode.greedy;

import org.junit.jupiter.api.Test;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.greedy
 * @Author :  cheungming
 * @CreateTime : 2024-06-08 10:43:59
 * @Description : 加油站
 * @Version : 1.0
 */
public class Code134Test {

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int currentGas = 0;
        int sumGas = 0;
        int start = 0;
        for (int i = 0; i < gas.length; i++) {
            currentGas += gas[i] - cost[i];
            sumGas += gas[i] - cost[i];
            if (currentGas < 0) {
                currentGas = 0;
                start = i + 1;
            }
        }
        if (sumGas < 0) {
            return -1;
        }

        return start;
    }

    @Test
    public void test1() {
        int[] gas = {1, 2, 3, 4, 5};
        int[] cost = {3, 4, 5, 1, 2};
        assert canCompleteCircuit(gas, cost) == 3;
    }


    @Test
    public void test2() {
        int[] gas = {2, 3, 4};
        int[] cost = {3, 4, 3};
        assert canCompleteCircuit(gas, cost) == -1;
    }
}
